package com.wangdong.mijiqiqi.notification;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.wangdong.mijiqiqi.R;

public class NotificationActivity extends AppCompatActivity {

    /**至于创建通知渠道的这部分代码，你可以写在MainActivity中，也可以写在Application中，
     * 实际上可以写在程序的任何位置，只需要保证在通知弹出之前调用就可以了。
     * 并且创建通知渠道的代码只在第一次执行的时候才会创建，以后每次执行创建代码系统会检测到该通知渠道已经存在了，
     * 因此不会重复创建，也并不会影响任何效率。
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        /*首先确定手机系统是8.0或则是更高*/
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            String channelId = "chat";
            String channelName = "聊天消息";
            /*其中App通知主要可以分为两类，一类是我和别人的聊天消息，这类消息非常重要，因此重要等级我设为了
            IMPORTANCE_HIGH。另一类是公众号的订阅消息，这类消息不是那么重要，因此重要等级我设为了
            IMPORTANCE_DEFAULT。除此之外，重要等级还可以设置为
            IMPORTANCE_LOW、IMPORTANCE_MIN，分别对应了更低的通知重要程度。*/
            int importance = NotificationManager.IMPORTANCE_HIGH;
            createNotificationChannel(channelId, channelName, importance);

            channelId = "subscribe";
            channelName = "订阅消息";
            importance = NotificationManager.IMPORTANCE_DEFAULT;
            createNotificationChannel(channelId, channelName, importance);
        }
    }
    /*首先确定手机系统是8.0或则是更高*/
    @TargetApi(Build.VERSION_CODES.O)
    private void createNotificationChannel(String channelId, String channelName, int importance) {
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
        NotificationManager notificationManager = (NotificationManager) getSystemService(
                NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(channel);
    }


    public void sendChatMsg(View view) {
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(this, "chat")
                .setContentTitle("收到一条聊天消息")
                .setContentText("今天中午吃什么？")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setAutoCancel(true)
                .build();
        manager.notify(1, notification);
    }

    public void sendSubscribeMsg(View view) {
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = new NotificationCompat.Builder(this, "subscribe")
                .setContentTitle("收到一条订阅消息")
                .setContentText("地铁沿线30万商铺抢购中！")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                .setAutoCancel(true)
                .build();
        manager.notify(2, notification);
    }


    /*如果在Android系统上实现未读角标的效果*/
    //    @TargetApi(Build.VERSION_CODES.O)
    //    private void createNotificationChannel(String channelId, String channelName, int importance) {
    //        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
    //        channel.setShowBadge(true);//表示允许显示未读角标
    //        NotificationManager notificationManager = (NotificationManager) getSystemService(
    //                NOTIFICATION_SERVICE);
    //        notificationManager.createNotificationChannel(channel);
    //    }
    //
    //    public void sendSubscribeMsg(View view) {
    //        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    //        Notification notification = new NotificationCompat.Builder(this, "subscribe")
    //                ...
    //                .setNumber(2)//未读数量
    //                .build();
    //        manager.notify(2, notification);
    //    }

}
